package com.wnn.demo.model.TaskApi.controller;

import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Accessory;
import com.wnn.demo.domain.Event;
import com.wnn.demo.domain.Task;
import com.wnn.demo.domain.User;
import com.wnn.demo.model.TaskApi.service.TaskApiService;
import com.wnn.demo.model.system.service.UserService;
import com.wnn.demo.model.task.service.AccessoryService;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.utils.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Api(value = "移动端任务信息 controller", tags = "移动端任务信息操作接口")
@RestController
@RequestMapping("/taskApp")
@AllArgsConstructor
public class TaskApiController {

    private TaskApiService taskApiService;

    @Autowired
    private UserService userService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private Accessory accessory;

    @Autowired
    private AccessoryService accessoryService;


    /**
     * 手机端未处理任务
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "手机端未处理任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = false, paramType = "query", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数", required = false, paramType = "query", dataType = "Integer", defaultValue = "10")
    })
    @GetMapping("/unfinishTask")
    public Result unfinishTask(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit){
        PageInfo<Task> taskPageInfo = taskApiService.unfinishTask(page, limit);

        return new Result(ResultCode.OK,taskPageInfo.getTotal(),taskPageInfo.getList());
    }

    /**
     * 已完成任务
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "手机端已完成任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = false, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数", required = false, dataType = "Integer", defaultValue = "10")
    })
    @GetMapping("/finishTask")
    public Result finishTask(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit){
        PageInfo<Task> taskPageInfo = taskApiService.finishTask(page, limit);

        return new Result(ResultCode.OK,taskPageInfo.getTotal(),taskPageInfo.getList());
    }

    @ApiImplicitParam(name = "id", value = "传入查看的任务id ", required = true, dataType = "String")
    @ApiOperation(value = "根据任务的 id 查看任务信息")
    @GetMapping("/checkTask")
    public Result checkTask(String id){
        Task task = taskApiService.checkTask(id);
        return new Result(ResultCode.OK,task);
    }
    /**
     * 接受任务
     * @param id
     * @return
     */
    @ApiImplicitParam(name = "id", value = "传入接收的任务id ", required = true, dataType = "String")
    @ApiOperation(value = "根据任务的 id 接受任务")
    @PostMapping("/acceptTask")
    public Result acceptTask(String id){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        User user = userService.findUserByName(username);
        Task task = new Task();
        task.setId(id);
        task.setUserId(user.getUserId());
        Integer i = taskApiService.acceptTask(task);
        if (i>0){
            return Result.ok();
        }else{
            return Result.failure();
        }
    }

    /**
     * 已接受任务
     * @return
     */
    @ApiOperation(value = "手机端已接受的任务信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = false, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页显示条数", required = false, dataType = "Integer", defaultValue = "10")
    })
    @GetMapping("/acceptedTask")
    public Result acceptedTask(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit){
        PageInfo<Task> taskPageInfo = taskApiService.acceptedTask(page, limit);

        return new Result(ResultCode.OK,taskPageInfo.getTotal(),taskPageInfo.getList());
    }

    /**
     * 事件上报
     * @param event
     * @return
     */
    @ApiOperation(value = "事件上报信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "reflector", value = "反映人", required = true, dataType = "String"),
            @ApiImplicitParam(name = "phoneNumber", value = "联系方式", required = true, dataType = "String"),
            @ApiImplicitParam(name = "location", value = "位置信息", required = true, dataType = "String"),
            @ApiImplicitParam(name = "description", value = "时间描述", required = true, dataType = "String"),
            @ApiImplicitParam(name = "filePaths", value = "附件路径", required = true, dataType = "String")
    })
    @PostMapping("/eventReport")
    public Result eventReport(Event event){
        String id = idWorker.nextId() + "";
        event.setId(id);
        Integer i = taskApiService.save(event);
        String filePaths = event.getFilePaths();
        String[] split = filePaths.split(",");
        for (String s : split) {
            String idd = idWorker.nextId() + "";
            accessory.setId(idd);
            accessory.setAccessoryId(id);
            accessory.setFilePath(s);
            accessoryService.saveFile(accessory);
        }
        if (i>0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @ApiImplicitParam(name = "file", value = "传入图片 ", required = true, dataType = "file")
    @ApiOperation(value = "传入图片")
    @PostMapping("/upload")
    public Result upload(MultipartFile file){

//		文件名
        String fileName = file.getOriginalFilename();
//		System.out.println("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
//		System.out.println("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String property = System.getProperty("user.dir");
//			String filePath = property+"/target/classes/picture/";
        String filePath = property+"/src/main/resources/public/picture/";
        String savePath = filePath+fileName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);

        } catch (IOException e) {
            e.printStackTrace();
            return Result.error();
        }
        return new Result(ResultCode.OK,savePath);
    }

    /**
     * 已处理事件信息
     * @return
     */
    @ApiImplicitParam(name = "id", value = "已处理事务的任务id ", required = true, dataType = "String")
    @ApiOperation(value = "根据任务的 id 接受任务")
    @GetMapping("/ProcessedTask")
    public Task ProcessedTask(String id){
        Task task = taskApiService.checkTask(id);
        Event event = taskApiService.findEvent(id);
        task.setEvent(event);
        return task;
    }

}
